package course17;

import java.io.*;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;

public class codeSearch {

public static void sereach() {
}
    public static void main(String[] args) throws Exception {
        //读取Downloads文件夹gaode.xlsx
        FileInputStream in = new FileInputStream(new File("G:/文档/gaode.xlsx"));
        Workbook workbook = new XSSFWorkbook(in);

        Sheet sheet = workbook.getSheetAt(0);

        int rowNum = sheet.getLastRowNum();
        Map<String, City> map = new HashMap<>();
        for (int i = 2; i < rowNum; i++) {
            City city = new City();
            Row row = sheet.getRow(i);
            String name = row.getCell(0).getStringCellValue();
            String abcode = row.getCell(1).getStringCellValue();
            city.setName(name);
            city.setAbcode(abcode);
            map.put(city.getAbcode(), city);
            //System.out.println(map.get(abcode).getAbcode());
            //System.out.println(map.get(abcode).getName());
        }
        //使用System.in创建BufferedReader
        BufferedReader bufferedReader1 = new BufferedReader(new InputStreamReader(System.in));
        String string = null;
        System.out.println("请输入要查询的城市的邮编,输入“结束”停止");
        do {
            try {
                string = (String) bufferedReader1.readLine();
                if (string.equals("结束")){}
                else if (!map.containsKey(string)) {
                System.out.println("未找到邮编对应城市，请输入正确的邮编");
                } else {
                    City city1 = map.get(string);
                    String youbian=city1.getAbcode();
                    char[] ch=youbian.toCharArray();
                    String yier=String.valueOf(ch[0])+ch[1];
                    String sansi=String.valueOf(ch[2])+ch[3];
                    String wuliu=String.valueOf(ch[4])+ch[5];
                    String zhixia=yier+"00"+"00";
                    String tebiexingzhenqu=yier+"00"+"00";
                    String sheng=yier+"00"+"00";
                    String didishi=yier+"01"+"00";
                    City sh=map.get(sheng);
                    City dishi=map.get(didishi);
                    City zhixiashi=map.get(zhixia);
                    City tebie=map.get(tebiexingzhenqu);


                    //如果邮编是直辖市
                    if (yier.equals("11")||yier.equals("12")||yier.equals("31")||yier.equals("50")){
                        if (sansi.equals("00")){
                            System.out.println("要查询的直辖市邮编是：" + city1.getAbcode());
                            System.out.println("要查询的邮编对应城市为：" + city1.getName());
                            System.out.println("它下辖的区有：");
                            //遍历区的名称
                            for (int i=0;i<99;i++){
                                String str=String.valueOf(i);
                                //str等于i的值，如果i<10，str=01
                                if (i<10){
                                    str="0"+str;
                                }
                                //qu直辖市下辖区的邮编
                                String qu=yier+"01"+str;
                                //重庆市下辖区的邮编
                                String qu2=yier+"02"+str;
                                //直辖市下辖区对应的城市
                                City city3=map.get(qu2);
                                City city2=map.get(qu);
                                //城市的非空判断
                                if (city2!=null){
                                    System.out.println("要查询的区邮编是：" + city2.getAbcode());
                                    System.out.println("要查询的区邮编对应城市为：" + city2.getName());
                                    System.out.println("     ");
                                }
                                //重庆市部分区的邮编
                                else if (city3!=null){
                                    System.out.println("要查询的区邮编是：" + city3.getAbcode());
                                    System.out.println("要查询的区邮编对应城市为：" + city3.getName());
                                    System.out.println("     ");
                                }

                            }
                        }
                        //如果是直辖市下辖区的邮编对应的城市不为空
                        else if (sansi.equals("01")||sansi.equals("02")){
                            System.out.println("要查询的区邮编是：" + city1.getAbcode());
                            System.out.println("要查询的邮编对应城市为：" + city1.getName());
                            System.out.println("该区属于的直辖市为："+zhixiashi.getName());
                        }
                        //特别行政区的查询
                    }else if (yier.equals("81")||yier.equals("82")) {
                        if (yier.equals("81") && wuliu.equals("00")) {
                            System.out.println("查询的特别行政区的邮编是：" + tebie.getAbcode());
                            System.out.println("查询的特别行政区为：" + tebie.getName());
                            System.out.println("他下辖的区有：");
                            for (int i = 1; i < 19; i++) {
                                String j = String.valueOf(i);
                                if (i < 10) {
                                    j = "0" + i;
                                }
                                String qu3 = yier + "00" + j;
                                City city4 = map.get(qu3);
                                if (city4 != null) {
                                    System.out.println("查询的特别行政区下辖区的邮编是：" + city4.getAbcode());
                                    System.out.println("查询的特别行政区下辖区为：" + city4.getName());
                                    System.out.println("      ");
                                }

                            }

                        } else if (yier.equals("81")) {
                            System.out.println("查询的特别行政区的邮编是：" + city1.getAbcode());
                            System.out.println("查询的特别行政区为：" + city1.getName());
                            System.out.println("它属于：" + tebie.getName());
                        } else if (yier.equals("82") && wuliu.equals("00")) {
                            System.out.println("查询的特别行政区的邮编是：" + tebie.getAbcode());
                            System.out.println("查询的特别行政区为：" + tebie.getName());
                            System.out.println("他下辖的区有：");
                            for (int i = 1; i < 9; i++) {
                                String j = String.valueOf(i);

                                String qu4 = yier + "000" + j;
                                City city5 = map.get(qu4);
                                if (city5 != null) {
                                    System.out.println("查询的特别行政区下辖区的邮编是：" + city5.getAbcode());
                                    System.out.println("查询的特别行政区下辖区为：" + city5.getName());
                                    System.out.println("      ");
                                }
                            }
                        }else {
                            System.out.println("查询的特别行政区下辖区的邮编是：" + city1.getAbcode());
                            System.out.println("查询的特别行政区下辖区为：" + city1.getName());
                            System.out.println("它属于："+tebie.getName());
                        }

                    }
                    //省份的查询
                    else {
                        if (sansi.equals("00")&&wuliu.equals("00")){
                            System.out.println("查询的省的邮编是："+city1.getAbcode());
                            System.out.println("查询的省为："+city1.getName());
                            System.out.println("它包含的市有：" );
                            for (int i=1;i<99;i++){
                              String a=String.valueOf(i);
                                if (i<10) {
                                    a="0"+i;
                                }
                                String shi1=yier+a+"00";
                                City city6=map.get(shi1);
                                if (city6!=null){
                                    System.out.println("查询的省的下辖市为："+city6.getName());
                                    System.out.println("查询的省的下辖市的邮编是："+city6.getAbcode());
                                    System.out.println("      ");
                                }

                            }
                        }else {
                            if (wuliu.equals("00")){
                                System.out.println("查询的下辖市为："+city1.getName());
                                System.out.println("查询的下辖市的邮编是："+city1.getAbcode());
                                System.out.println("查询的下辖市包含的市县区有：");
                                for (int i=1;i<99;i++){
                                    String a=String.valueOf(i);
                                    if (i<10) {
                                        a="0"+i;
                                    }
                                    String shixianqu=yier+ sansi+a;
                                    City city7=map.get(shixianqu);
                                    if (city7!=null){
                                        System.out.println("查询的市的包含的市县有："+city7.getName());
                                        System.out.println("查询的市包含的市辖区邮编是："+city7.getAbcode());
                                        System.out.println("      ");
                                    }
                                }

                            }else{
                                String f=yier+sansi+"00";
                                City city=map.get(f);
                                System.out.println("查询的城市为："+city1.getName());
                                System.out.println("查询的城市的邮编是："+city1.getAbcode());
                                System.out.println("它属于的市为："+city.getName());
                            }
                        }

                    }

                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } while (!string.equals("结束"));
    }

}
